{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pregnants_tfidf</th>\n",
       "      <th>Plasma_glucose_concentration_tfidf</th>\n",
       "      <th>blood_pressure_tfidf</th>\n",
       "      <th>Triceps_skin_fold_thickness_tfidf</th>\n",
       "      <th>serum_insulin_tfidf</th>\n",
       "      <th>BMI_tfidf</th>\n",
       "      <th>Diabetes_pedigree_function_tfidf</th>\n",
       "      <th>Age_tfidf</th>\n",
       "      <th>Target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.293647</td>\n",
       "      <td>0.389263</td>\n",
       "      <td>0.068664</td>\n",
       "      <td>0.416311</td>\n",
       "      <td>-0.317941</td>\n",
       "      <td>0.093614</td>\n",
       "      <td>0.214973</td>\n",
       "      <td>0.654334</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.458093</td>\n",
       "      <td>-0.609101</td>\n",
       "      <td>-0.087047</td>\n",
       "      <td>0.287852</td>\n",
       "      <td>-0.375682</td>\n",
       "      <td>-0.371091</td>\n",
       "      <td>-0.197934</td>\n",
       "      <td>-0.103381</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.408951</td>\n",
       "      <td>0.644218</td>\n",
       "      <td>-0.087479</td>\n",
       "      <td>-0.426959</td>\n",
       "      <td>-0.229648</td>\n",
       "      <td>-0.365657</td>\n",
       "      <td>0.200318</td>\n",
       "      <td>-0.034994</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.425010</td>\n",
       "      <td>-0.502137</td>\n",
       "      <td>-0.080761</td>\n",
       "      <td>0.077736</td>\n",
       "      <td>0.062026</td>\n",
       "      <td>-0.248523</td>\n",
       "      <td>-0.463179</td>\n",
       "      <td>-0.523940</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.186954</td>\n",
       "      <td>0.082528</td>\n",
       "      <td>-0.246360</td>\n",
       "      <td>0.148546</td>\n",
       "      <td>0.125389</td>\n",
       "      <td>0.230816</td>\n",
       "      <td>0.898036</td>\n",
       "      <td>-0.003356</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   pregnants_tfidf  Plasma_glucose_concentration_tfidf  blood_pressure_tfidf  \\\n",
       "0         0.293647                            0.389263              0.068664   \n",
       "1        -0.458093                           -0.609101             -0.087047   \n",
       "2         0.408951                            0.644218             -0.087479   \n",
       "3        -0.425010                           -0.502137             -0.080761   \n",
       "4        -0.186954                            0.082528             -0.246360   \n",
       "\n",
       "   Triceps_skin_fold_thickness_tfidf  serum_insulin_tfidf  BMI_tfidf  \\\n",
       "0                           0.416311            -0.317941   0.093614   \n",
       "1                           0.287852            -0.375682  -0.371091   \n",
       "2                          -0.426959            -0.229648  -0.365657   \n",
       "3                           0.077736             0.062026  -0.248523   \n",
       "4                           0.148546             0.125389   0.230816   \n",
       "\n",
       "   Diabetes_pedigree_function_tfidf  Age_tfidf  Target  \n",
       "0                          0.214973   0.654334       1  \n",
       "1                         -0.197934  -0.103381       0  \n",
       "2                          0.200318  -0.034994       1  \n",
       "3                         -0.463179  -0.523940       0  \n",
       "4                          0.898036  -0.003356       1  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv(\"./data/FE_diabetes-tfidf.csv\")\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train = train['Target']   \n",
    "X_train = train.drop([\"Target\"], axis=1)\n",
    "\n",
    "feat_names = X_train.columns \n",
    "\n",
    "from scipy.sparse import csr_matrix\n",
    "X_train = csr_matrix(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "lr = LogisticRegression()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('accuracy of each fold is: ', array([0.75974026, 0.72727273, 0.73376623, 0.82352941, 0.77124183]))\n",
      "('cv accuracy is:', 0.7631100925218572)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "loss = cross_val_score(lr, X_train, y_train, cv=5, scoring='accuracy')\n",
    "print ('accuracy of each fold is: ',loss)\n",
    "print ('cv accuracy is:', loss.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise-deprecating',\n",
       "       estimator=LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
       "          intercept_scaling=1, max_iter=100, multi_class='warn',\n",
       "          n_jobs=None, penalty='l2', random_state=None, solver='liblinear',\n",
       "          tol=0.0001, verbose=0, warm_start=False),\n",
       "       fit_params=None, iid='warn', n_jobs=4,\n",
       "       param_grid={'penalty': ['l1', 'l2'], 'C': [0.1, 1, 10, 100, 1000]},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n",
       "       scoring='accuracy', verbose=0)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "penaltys = ['l1','l2']\n",
    "Cs = [ 0.1, 1, 10, 100, 1000]\n",
    "tuned_parameters = dict(penalty = penaltys, C = Cs)\n",
    "\n",
    "lr_penalty= LogisticRegression(solver='liblinear')\n",
    "grid= GridSearchCV(lr_penalty, tuned_parameters,cv=5, scoring='accuracy',n_jobs = 4,)\n",
    "grid.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7669270833333334\n",
      "{'penalty': 'l2', 'C': 0.1}\n"
     ]
    }
   ],
   "source": [
    "# examine the best model\n",
    "print(grid.best_score_)\n",
    "print(grid.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZQAAAEKCAYAAAA1qaOTAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xl4XPV97/H3V9uMNi9aLAsbWwZMKMaxAZGasBTbULhAAk7M1hIIjXHSG5o0t6Vxm5Y0SdPS9j5JnrRZcGiI03JZshgcVhuHJaQBYsAEsxQTYxMbL7K8aV+/949zJI2sGWkkn9HI8uf1PPPMWX5n5jtjyR+d3znnd8zdEREROVI52S5ARETGBwWKiIhEQoEiIiKRUKCIiEgkFCgiIhIJBYqIiERCgSIiIpFQoIiISCQUKCIiEom8bBcwmioqKrympibbZYiIHFVefPHFve5eOVS7YypQampq2LBhQ7bLEBE5qpjZtnTaqctLREQioUAREZFIKFBERCQSx9QxFBGRHh0dHWzfvp3W1tZslzJmxONxpk+fTn5+/oi2V6CIyDFp+/btlJaWUlNTg5llu5ysc3fq6+vZvn07s2bNGtFrqMtLRI5Jra2tlJeXK0xCZkZ5efkR7bEpUETkmKUw6e9Ivw8FiohImq6541dcc8evsl3GmKVAkcjpl04kPSUlJb3Tl1xyCZMmTeLyyy9P2vbTn/408+fP59RTT6WwsJD58+czf/58fvzjHw/rPV966SUee+yxI6o7FR2UFxEZA2699Vaam5u54447kq7/1re+BcDWrVu5/PLL2bhx44je56WXXmLTpk1ccsklI641Fe2hiIiMAYsXL6a0tHRE227evJmLL76YM888k/PPP5+33noLgHvvvZfTTjuNefPmsXDhQlpaWvjyl7/M3XffPaK9m6FoD0VEjnlf+tlrvP7eoSHbvb4zaJNOl+6px03gix+ac8S1pWP58uXceeednHjiifzyl7/klltuYe3atXzpS1/iqaeeoqqqigMHDlBYWMhtt93Gpk2b+MY3vhF5HQqUNPT88Nz3ybOzXImMR6/947kAzPmbZ7NcydFB31d/Bw4c4LnnnuOjH/1o77LOzk4AzjnnHG644QauuuoqPvKRj2S8FgWKiBzz0t2TGIt/XLo7FRUVSY+pfO973+P555/noYce4owzzuDll1/OaC06hiIichSbPHky1dXVrF69GoDu7m5eeeUVALZs2cKCBQv4yle+wuTJk9mxYwelpaU0NDRkpBYFiojIGHDeeedx1VVXsX79eqZPn87jjz+e9rb33nsv3/3ud5k3bx5z5szhoYceAuBzn/scc+fOZe7cuSxcuJDTTjuNRYsW8corr3D66afroLyIyHjR2NjYO/2LX/wirW1qamrYtGlTv2UnnHBC0gBas2bNgGWVlZUZu9GgAkVEJE1j6djJ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oYiIiKRUKCIiEgkFCgiIhIJBYqIiERCgSIiIpFQoIiISCT+P/yjb0YFcKRLAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot CV误差曲线\n",
    "test_means = grid.cv_results_[ 'mean_test_score' ]\n",
    "test_stds = grid.cv_results_[ 'std_test_score' ]\n",
    "train_means = grid.cv_results_[ 'mean_train_score' ]\n",
    "train_stds = grid.cv_results_[ 'std_train_score' ]\n",
    "\n",
    "# plot results\n",
    "n_Cs = len(Cs)\n",
    "number_penaltys = len(penaltys)\n",
    "test_scores = np.array(test_means).reshape(n_Cs,number_penaltys)\n",
    "train_scores = np.array(train_means).reshape(n_Cs,number_penaltys)\n",
    "test_stds = np.array(test_stds).reshape(n_Cs,number_penaltys)\n",
    "train_stds = np.array(train_stds).reshape(n_Cs,number_penaltys)\n",
    "\n",
    "x_axis = np.log10(Cs)\n",
    "for i, value in enumerate(penaltys):\n",
    "    #pyplot.plot(log(Cs), test_scores[i], label= 'penalty:'   + str(value))\n",
    "    plt.errorbar(x_axis, -test_scores[:,i], yerr=test_stds[:,i] ,label = penaltys[i] +' Test')\n",
    "    #plt.errorbar(x_axis, -train_scores[:,i], yerr=train_stds[:,i] ,label = penaltys[i] +' Train')\n",
    "    \n",
    "plt.legend()\n",
    "plt.xlabel( 'log(C)' )                                                                                                      \n",
    "plt.ylabel( 'accuracy' )\n",
    "#plt.savefig('LogisticGridSearchCV_C.png' )\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cPickle\n",
    "\n",
    "cPickle.dump(grid.best_estimator_, open(\"L2_tfidf_accuracy.pkl\", 'wb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
